angular.module('Controller')

.controller('WFResourceManageCtrl',($scope,NgTableParams,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http)->
    initSearchData = ()->
        $scope.search = {
            enabled:true
        }
    initSearchData()
    $scope.enabledList=commonConstant.enabledList(true)
    $scope.resourceTypeList=commonConstant.resourceTypeList(true)
    $scope.tableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: [5, 10, 20],
                getData: ($defer, params)->
                    $scope.doQuery($defer,params)
                    return ;
            }
    )
    $scope.doQuery=($defer, params)->
        requestData={
            page: params.page() - 1,
            size: params.count(),
            enabled:$scope.search.enabled,
            resourceLike: $scope.search.name,
            accountId:$localStorage['accountId']
        }
        getParam=commonUtil.transferGetParam(requestData)
        $http.get('@ostengar/resource'+getParam)
        .success (resp)->
            if resp.code==0
                $scope.tableData=resp.message.content
                params.total(resp.message.totalElements)
                $defer.resolve($scope.tableData)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    _.extend $scope,
        newData: ->
            data={name:'',type:'uri',method:'GET',path:'/menu',enabled:true}
            showModal('new',data)
        editData:(data)->
            theData=commonUtil.cloneObj(data)
            showModal('edit',theData)
        resetSearch: ->
            initSearchData()
        queryData: (firstPage)->
            if(firstPage == true)
                $scope.tableParams.page(1)
            $scope.tableParams.reload()
    showModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'WFResourceManageEditCtrl',
            templateUrl: 'templates/system/resource/resource-manage-modal.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.queryData()
        )
)
